package com.atguigu.web.admin.controller.apartment;


import com.atguigu.common.result.Result;
import com.atguigu.model.entity.RoomInfo;
import com.atguigu.model.enums.ReleaseStatus;
import com.atguigu.web.admin.service.RoomInfoService;
import com.atguigu.web.admin.vo.room.RoomDetailVo;
import com.atguigu.web.admin.vo.room.RoomItemVo;
import com.atguigu.web.admin.vo.room.RoomQueryVo;
import com.atguigu.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "房间信息管理")//名字是房间信息管理，所以相应注入房间信息
//的Service层
@RestController
@RequestMapping("/admin/room")
public class RoomController {

    @Autowired
    private RoomInfoService roomInfoService;

    @Operation(summary = "保存或更新房间信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody RoomSubmitVo roomSubmitVo) {
        roomInfoService.saveOrUpdateRoom(roomSubmitVo);
        return Result.ok();
    }

    @Operation(summary = "根据条件分页查询房间列表")
    @GetMapping("pageItem")
    //前台传入current当前页码、size当前条数、queryVo前端传的查询信息
    public Result<IPage<RoomItemVo>> pageItem(@RequestParam long current, @RequestParam long size, RoomQueryVo queryVo) {
        //创建一个分页查询对象，泛型参数 RoomItemVo 表示分页查询的结果数据类型。current 参数表示当前页码，size 参数表示每页显示的记录数。
        IPage<RoomItemVo> page = new Page<>(current,size);
//        运用房间信息的Service层的分页查询方法，用page分页插件操作queryVo，得到Result（一系列RoomItemVo），返回前端
        IPage<RoomItemVo> result = roomInfoService.pageRoomItemByQuery(page,queryVo);
        return Result.ok(result);
    }

    @Operation(summary = "根据id获取房间详细信息")
    @GetMapping("getDetailById")
    public Result<RoomDetailVo> getDetailById(@RequestParam Long id) {
        RoomDetailVo roomDetailVo = roomInfoService.getRoomDetailById(id);
        return Result.ok(roomDetailVo);
    }

    @Operation(summary = "根据id删除房间信息")
    @DeleteMapping("removeById")
    public Result removeById(@RequestParam Long id) {
        roomInfoService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id修改房间发布状态")
    @PostMapping("updateReleaseStatusById")
    public Result updateReleaseStatusById(Long id, ReleaseStatus status) {
//      房间发布状态属性存在RoomInfo的isRelease里
//        新建条件更新器
        LambdaUpdateWrapper<RoomInfo> updateWrapper = new LambdaUpdateWrapper<>();
//        要求查找房间id等于前端传的id的房间
        updateWrapper.eq(RoomInfo::getId,id);
//        要求设置房间的发布状态为前端传的状态
        updateWrapper.set(RoomInfo::getIsRelease,status);
//        调用service层继承Iservice带来的update方法，将条件更新器的条件赋予
        roomInfoService.update(updateWrapper);
        return Result.ok();
    }

    @GetMapping("listBasicByApartmentId")
    @Operation(summary = "根据公寓id查询房间列表")
    public Result<List<RoomInfo>> listBasicByApartmentId(Long id) {
//        启动条件查询器
        LambdaQueryWrapper<RoomInfo> queryWrapper = new LambdaQueryWrapper<>();
//        设置好条件查询器的要求，一是要房间的公寓id=前端传的id
        queryWrapper.eq(RoomInfo::getApartmentId,id);
//        二是要房间状态等于状态枚举类中的已发布
        queryWrapper.eq(RoomInfo::getIsRelease,ReleaseStatus.RELEASED);
//        用service的list方法找到符合条件查询器的结果
        List<RoomInfo> roomInfoList = roomInfoService.list(queryWrapper);
//        返回结果给前端
        return Result.ok(roomInfoList);
    }

}


















